﻿using Daemon.Model;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Common;
using System.Net;

namespace DaemonAPI
{
	[ApiController]
	[Route("api/[controller]/[action]")]
	public class TakeAwayUserCouponController : BaseApiController<TakeAwayUserCoupon, ITakeAwayUserCouponRepository>
	{
		private readonly ITakeAwayCouponRepository _takeAwayCouponRepository;
		public TakeAwayUserCouponController(ITakeAwayCouponRepository takeAwayCouponRepository, ITakeAwayUserCouponRepository repository, IHttpContextAccessor httpContextAccessor) : base(repository, httpContextAccessor)
		{
			_takeAwayCouponRepository = takeAwayCouponRepository;
		}

		[HttpPost]
		[Route("")]
		public ResultModel TakeCoupon(int couponId)
		{
			if (!UserId.HasValue)
			{
				return new ResultModel(HttpStatusCode.BadRequest, "领取失败！", null);
			}

			var couponInfo = _takeAwayCouponRepository.FindById(couponId);
			var takeAwayUserCoupon = new TakeAwayUserCoupon() { CouponId = couponId, UserId = UserId.Value };

			_repository.AddByDBContext(takeAwayUserCoupon);
			return new ResultModel(HttpStatusCode.OK, "领取成功！");
		}
	}
}
